﻿using DoxygenBrowser.Common.Parser;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using DoxygenBrowser.Common.InOut;
using DoxygenBrowser.Common.Model;
using System.Text;
using System;
using System.Diagnostics;
using TestDoxTokenParser.Properties;

namespace TestDoxTokenParser
{


  [TestClass()]
  public class DoxTokenParserTest
  {
    public class TestLoader
    {
      private int _iNumEntries;
      TDictionaryFile _tDictionaryFile;

      public TestLoader()
      {
        _tDictionaryFile = new TDictionaryFile();
      }

      public TDictionaryFile TDictionaryFile
      {
        get { return _tDictionaryFile; }
      }

      public void HandleTokenAdded(object sender, TokenAddedEventArgs e)
      {
        _tDictionaryFile.AddEntry(e.TokenIdentifier.GetNormName(), e.FileLocation.Path, e.FileLocation.Anchor);
        _iNumEntries++;
      }

      public int NumEntries
      {
        get { return _iNumEntries; }
      }

      private void DebugTokenEntry(TokenIdentifier tokenId, FileLocation fileLocation)
      {
        Debug.Print("---------------------------------------------------------------");
        Debug.Print("{0} {1} {2} {3}", tokenId.Name, tokenId.Scope, tokenId.Type, tokenId.APILanguage);
        Debug.Print("{0} {1}", fileLocation.Path, fileLocation.Anchor);
        Debug.Print("\n");
      }



    }

    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion

    private TestLoader createTDictionaryFromString(string strXmlTokens)
    {
      TestLoader testLoader = new TestLoader();
      TokenFileParser target = new TokenFileParser();
      Stream memoryStream = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(strXmlTokens));
      target.TokenAddedEvent += new EventHandler<TokenAddedEventArgs>(testLoader.HandleTokenAdded);
      target.Parser(memoryStream);
      return testLoader;
    }

    private TestLoader parserFromFile(string strFilePath)
    {
      TestLoader testLoader = new TestLoader();
      TokenFileParser target = new TokenFileParser();
      target.TokenAddedEvent += new EventHandler<TokenAddedEventArgs>(testLoader.HandleTokenAdded);
      StreamReader txtReader = new StreamReader(strFilePath, Encoding.ASCII);

      Assert.IsNotNull(txtReader);
      target.Parser(txtReader);

      return testLoader;

    }


    /// <summary>
    ///A test for Parser
    ///</summary>
    [TestMethod()]
    public void ParserTest()
    {
      TestLoader testLoader = createTDictionaryFromString(Resources.Tokens);
      Assert.IsTrue(testLoader.NumEntries == 20);
    }


    /// <summary>
    /// Parsers the empty token list test.
    /// </summary>
    [TestMethod()]
    public void Parser_EmptyTokenListTest()
    {
      TestLoader testLoader = createTDictionaryFromString(Resources.Tokens_EmptyList);
      Assert.IsTrue(testLoader.NumEntries == 0);
    }


    /// <summary>
    /// Parser one token list test.
    /// </summary>
    [TestMethod()]
    public void Parser_1TokenListTest()
    {

      TestLoader testLoader = createTDictionaryFromString(Resources.Tokens_1Token);
      Assert.IsTrue(testLoader.NumEntries == 1);

    }

    /// <summary>
    /// Parser one token list test.
    /// </summary>
    [TestMethod()]
    public void Parser_WithSpecialCharsTest()
    {
      TestLoader testLoader =
          parserFromFile(@"C:\jgallardo\Projects\DoxygenAddin\TestDoxygenParser\Resources\Tokens_SpecialChars.xml");

      Assert.IsTrue(testLoader.NumEntries == 2);
    }


    /// <summary>
    /// Parser_s huge token list test.
    /// </summary>
    [TestMethod()]
    public void Parser_HugeTokenListTest()
    {
      TestLoader testLoader =
        parserFromFile(@"C:\jgallardo\Projects\DoxygenAddin\TestDoxygenParser\Resources\Tokens_huge.xml");

      Assert.IsTrue(testLoader.NumEntries > 0);
    }


  }
}
